package com.abredatos.activities;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.abredatos.Main;
import com.abredatos.R;
import com.abredatos.test.HelloItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class CustomMapActivity extends MapActivity
{
	LocationManager mlocManager ;
	private MapController myMapController;
	LocationListener mlocListener;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

		MapView mapView = (MapView) findViewById(R.id.mapview1);
		mapView.setBuiltInZoomControls(true);

		mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        mlocListener = new MyLocationListener(); 
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
        
		//myLongitude = (TextView)findViewById(R.id.longitude);
		//myLatitude = (TextView)findViewById(R.id.latitude);
		myMapController = mapView.getController();
		myMapController.setZoom(5); //Fixed Zoom Level
		
		/*
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

		GeoPoint point = new GeoPoint(30443769,-91158458);
		OverlayItem overlayitem = new OverlayItem(point, "Laissez les bon temps rouler!", "I'm in Louisiana!");

		GeoPoint point2 = new GeoPoint(17385812,78480667);
		OverlayItem overlayitem2 = new OverlayItem(point2, "Namashkaar!", "I'm in Hyderabad, India!");

		itemizedoverlay.addOverlay(overlayitem);
		itemizedoverlay.addOverlay(overlayitem2);
		
		
		mapOverlays.add(itemizedoverlay);
*/		

		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.cat101);
		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
		
		 for (int i=0; i < Main.obrasPlanE.size() ;i+=1) {
			 String pruebas = Main.obrasPlanE.get(i).getUrlProyecto();
	        	String [] prueba  = pruebas.split(">");
	        	prueba = prueba[1].split("</a");
	        	pruebas = prueba[0];
	        	
	        	
			 GeoPoint point = new GeoPoint((int)(Main.obrasPlanE.get(i).getCoordinateX()*1000000),(int)(Main.obrasPlanE.get(i).getCoordinateY()*1000000));
			 Log.i("AbreDatos", "coordX " + (int)(Main.obrasPlanE.get(i).getCoordinateX()*1000000));
			 OverlayItem overlayitem = new OverlayItem(point, pruebas, "Presupuesto: "+Main.obrasPlanE.get(i).getPresupuesto());
			 itemizedoverlay.addOverlay(overlayitem);
		 }
		 
		mapOverlays.add(itemizedoverlay);
		
	}
	/* Class My Location Listener */
	public class MyLocationListener implements LocationListener
	{

			public void onLocationChanged(Location loc)
			{
				// TODO Auto-generated method stub
				   GeoPoint myGeoPoint = new GeoPoint(
				    (int)(loc.getLatitude()*1000000),
				    (int)(loc.getLongitude()*1000000));
				   
				 //  CenterLocatio(myGeoPoint);
			}
 
			public void onProviderDisabled(String provider)
			{
				Toast.makeText( getApplicationContext(),
						"Gps Disabled",
						Toast.LENGTH_SHORT ).show();
			}
 
			public void onProviderEnabled(String provider)
			{
				Toast.makeText( getApplicationContext(),
						"Gps Enabled",
						Toast.LENGTH_SHORT).show();
			}
 
			
			public void onStatusChanged(String provider, int status, Bundle extras)
			{
 
			}
	}/* End of Class MyLocationListener */
	@Override
	protected void onPause() {
		super.onPause();
		// Remove the listener you previously added
		mlocManager.removeUpdates(mlocListener);
		this.finish();
	}

	@Override
    protected void onStop() {
		super.onStop();
		mlocManager.removeUpdates(mlocListener);
    	this.finish();
    }

	@Override
    protected void onDestroy() {
		super.onDestroy();
		mlocManager.removeUpdates(mlocListener);
    	this.finish();
    }
	
	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}
	
	private void CenterLocatio(GeoPoint centerGeoPoint)
	 {
	  myMapController.animateTo(centerGeoPoint);
	  
	 
	/*  myLongitude.setText("Longitude: "+
	   String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
	   );
	  myLatitude.setText("Latitude: "+
	   String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
	   );*/
	 };
	}